home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / CD32 / CD32_Support / examples / SA_Examples / nonvolatile / GetNVList.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-31  |  6.5 KB  |  249 lines

  1. /* GetNVList.c
  2.  * 
  3.  * Test nonvolatile.library/GetNVList()
  4.  * 
  5.  * 
  6.  */
  7.  
  8. /* Includes --------------------------------------------- */
  9. #include <exec/types.h>
  10. #include <exec/lists.h>
  11. #include <exec/libraries.h>
  12. #include <exec/memory.h>
  13. #include <dos/dos.h>
  14. #include <dos/rdargs.h>
  15. #include <libraries/nonvolatile.h>
  16. #include <clib/exec_protos.h>
  17. #include <clib/dos_protos.h>
  18. #include <clib/nonvolatile_protos.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21.  
  22. #include <pragmas/nonvolatile_pragmas.h>
  23.  
  24. #include "nv.h"       /* default names, etc. */
  25.  
  26. /* Defines ------------------------------------------ */
  27. #define PROGNAME "GetNVList"
  28. #define ERRMSG_LIBNOOPEN "Couldn't open %s V%ld (or better)!\n"
  29. #define TEMPLATE "NAME=APPNAME/K,NULL=NULLTEST/S,KILLREQ/S,NOBREAK/S"
  30.  
  31. /* #define DEBUG 1 */
  32.  
  33. /* Structs ------------------------------ */
  34. struct Opts {
  35.     STRPTR name;
  36.     LONG nullTest;
  37.     LONG killReq;
  38.     LONG noBreak;
  39. };
  40.  
  41. /* Protos ------------------------------------------ */
  42. static VOID GoodBye(int);
  43. static LONG doInit(VOID);
  44. static VOID dumpNVE(struct NVEntry *);
  45. #ifdef DEBUG
  46. extern VOID KPrintF(STRPTR,...);
  47. static VOID kDumpNVE(struct NVEntry *);
  48. #endif
  49.  
  50. /* Globals --------------------------------------- */
  51. struct Opts          opts;
  52. struct RDArgs       *rdargs;
  53. struct Library      *NVBase;
  54. struct MinList      *ml;
  55. UBYTE  blankStr[] = "";
  56.  
  57. VOID main(int argc, UBYTE *argv[]) {
  58.     STRPTR name, prName;
  59.     struct NVEntry *nve;
  60.     int rc = RETURN_OK;
  61.     BOOL killReq;
  62.  
  63.     if (!doInit()) {
  64.         GoodBye(RETURN_FAIL);
  65.     }
  66.  
  67.     name = ((opts.name) ? opts.name : (STRPTR)DEFAULT_APP_NAME);
  68.     if (opts.nullTest) {
  69.         name = NULL;
  70.     }
  71.  
  72.     prName = ((name) ? name : blankStr);  /* originally for debug, needed by strcmp below */
  73.  
  74.     killReq = ((opts.killReq) ? TRUE : FALSE);
  75.  
  76.     Printf("Calling GetNVList('%s', %ld)...\n", prName, (LONG)killReq);
  77. #ifdef DEBUG
  78.     KPrintF("Calling GetNVList('%s', %ld)...\n", prName, (LONG)killReq);
  79. #endif
  80.     
  81.     if (ml = GetNVList(name, killReq)) {
  82. #ifdef DEBUG
  83.         KPrintF("GetNVList returned $%08lx\n", ml);
  84. #endif
  85.         Printf("Got NVList at $%08lx\n", ml);
  86.         nve = (struct NVEntry *)(ml->mlh_Head);
  87.         if (!nve) {
  88.             PutStr("GetNVList() returned a Headless List!!\n");
  89.             rc = RETURN_ERROR;
  90.         }
  91.         while (nve) {
  92.             if (CheckSignal(SIGBREAKF_CTRL_C)) {
  93.                 GoodBye(rc);
  94.             }
  95.  
  96. #ifdef DEBUG
  97.             KPrintF("size: %ld, nvename %08lx, name %08lx, break %08lx\n",
  98.                      (LONG)(nve->nve_Size),
  99.                      nve->nve_Name,
  100.                      name,
  101.                      opts.noBreak);
  102. #endif
  103.             if (!TypeOfMem(nve->nve_Name)) {
  104.                 if (nve->nve_Node.mln_Succ) {
  105.                     Printf("Bogus name pointer detected: $%08lx\n", nve->nve_Name);
  106. #ifdef DEBUG
  107.                     KPrintF("Bogus name pointer detected: $%08lx\n", nve->nve_Name);
  108. #endif
  109.                 }
  110.                 else {
  111.                     Printf("End of list detected, name: $%08lx\n", nve->nve_Name);
  112. #ifdef DEBUG
  113.                     KPrintF("End of list detected, name: $%08lx\n", nve->nve_Name);
  114. #endif
  115.                 }
  116.             }
  117.  
  118.             /*  (size==0 && nve_name==prName) && (nobreak unspecified) */
  119.             if (((nve->nve_Size == 0UL) && (!strcmp(nve->nve_Name,prName))) && (!opts.noBreak)) {
  120.                 break;
  121.             }
  122.             
  123.             /* and the last entry in the list tends to be bogus... */
  124.             if ((NULL == nve->nve_Node.mln_Succ) && (!opts.noBreak)) {
  125.                 break;
  126.             }
  127.  
  128.             /* print out contents of the nve we found */
  129. #ifdef DEBUG
  130.             kDumpNVE(nve);
  131. #endif
  132.             dumpNVE(nve);
  133.  
  134.             nve = (struct NVEntry *)(nve->nve_Node.mln_Succ);
  135.         }
  136.         PutStr("Calling FreeNVData()...\n");
  137.         FreeNVData(ml);
  138.         ml = NULL;
  139.     }
  140.     else {
  141.         Printf("GetNVList('%s', %ld) returned NULL!\n", prName, killReq);
  142.     }
  143.  
  144.  
  145.     GoodBye(rc);
  146. }
  147.  
  148. /* dumpNVE ==============================================
  149.    print out the contents of a struct NVEntry
  150.  */
  151. static VOID dumpNVE(struct NVEntry *nv) {
  152.  
  153.     if (!nv) {
  154.         PutStr("dumpNVE() received a null pointer for printing!\n");
  155.         return;
  156.     }
  157.     if (!TypeOfMem(nv)) {
  158. #ifdef DEBUG
  159.         KPrintF("dumpNVE() received a bogus pointer for printing!\n");
  160. #endif
  161.         PutStr("dumpNVE() received a bogus pointer for printing!\n");
  162.     }
  163.  
  164.     Printf("NVEntry at $%08lx:  Succ: $%08lx  Pred: $%08lx\n", 
  165.             nv,
  166.             nv->nve_Node.mln_Succ,
  167.             nv->nve_Node.mln_Pred);
  168.     if ((nv->nve_Node.mln_Succ) && (TypeOfMem(nv->nve_Node.mln_Succ))) {
  169.         Printf("  Name: '%s' ", nv->nve_Name);
  170.     }
  171.     else {
  172.         Printf("  Name: $%08lx ", nv->nve_Name);
  173.     }
  174.     Printf("  Size:  %4lu Prot:  $%lx\n\n", nv->nve_Size, nv->nve_Protection);
  175.  
  176.     return;
  177. }
  178. #ifdef DEBUG
  179. /* kDumpNVE ==================================================================
  180.    kprintf version of dumpnve 
  181.  */
  182. static VOID kDumpNVE(struct NVEntry *nv) {
  183.  
  184.     if (!nv) {
  185.         KPrintF("dumpNVE() received a null pointer for printing!\n");
  186.         return;
  187.     }
  188.     if (!TypeOfMem(nv)) {
  189.         KPrintF("dumpNVE() received a bogus pointer for printing!\n");
  190.     }
  191.     KPrintF("NVEntry at $%08lx:  Succ: $%08lx\n     Pred: $%08lx\n", 
  192.              nv,
  193.             nv->nve_Node.mln_Succ,
  194.             nv->nve_Node.mln_Pred);
  195.     if ((nv->nve_Node.mln_Succ) && (TypeOfMem(nv->nve_Node.mln_Succ))) {
  196.         KPrintF("  Name: '%s' ", nv->nve_Name);
  197.     }
  198.     else {
  199.         KPrintF("  Name: $%08lx ", nv->nve_Name);
  200.     }
  201.     KPrintF("  Size:  %4lu  Prot:  $%lx\n\n", 
  202.                nv->nve_Size, nv->nve_Protection);
  203.  
  204.     return;
  205. }
  206. #endif
  207.  
  208. /* GoodBye ===============================================
  209.    Clean-exit routine.
  210.  */
  211. static VOID GoodBye(int rc) {
  212.  
  213.     if ((ml) && (TypeOfMem(ml))) {
  214.         Printf("Calling FreeNVData(ml=$%08lx)...\n", ml);
  215.         FreeNVData(ml);
  216.     }
  217.  
  218.     if (NVBase) {
  219.         CloseLibrary(NVBase);
  220.     }
  221.  
  222.     if (rdargs) {
  223.         FreeArgs(rdargs);
  224.     }
  225.  
  226.     exit(rc);
  227. }
  228.  
  229. /* doInit =============================================
  230.  * Open libraries, call ReadArgs() if necessary.
  231.  * Returns TRUE for success, FALSE otherwise.
  232.  */
  233. static LONG doInit(VOID) {
  234.     rdargs = ReadArgs(TEMPLATE, (LONG *)&opts, NULL);
  235.     if (!rdargs) {
  236.         PrintFault(IoErr(), PROGNAME);
  237.         return(FALSE);
  238.     }
  239.  
  240.     NVBase = OpenLibrary("nonvolatile.library", 40L);
  241.     if (!NVBase) {
  242.         Printf(ERRMSG_LIBNOOPEN, "nonvolatile.library", 40L);
  243.         return(FALSE);
  244.     }
  245.  
  246.     return(TRUE);
  247. }
  248.  
  249.